<?php
class ControllerSalePromotion extends Controller {
	private $error = array();
	
  	public function index() {
		$this->load->language('sale/promotion');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/promotion');
    	$this->getList();
  	}
	
  	public function insert() {
		$this->load->language('sale/promotion');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/promotion');
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$post_data = $this->request->post;
			$params_data = array(
				'name'       => $post_data['name'],
				'image'      => $post_data['image'],
				'start_time' => strtotime($post_data['start_time']),
				'end_time'   => strtotime($post_data['end_time']),
				'status'     => $post_data['status'],
				'promotion_description' => $post_data['promotion_description']
			);
      	  	$this->model_sale_promotion->addPromotion($params_data);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('sale/promotion', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getForm_text();
    	$this->getForm_insert();
  	}
	
  	public function update() {
		$this->load->language('sale/promotion');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('sale/promotion');
		if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
			$post_data = $this->request->post;
			$params_data = array(
				'name'       => $post_data['name'],
				'image'      => $post_data['image'],
				'start_time' => strtotime($post_data['start_time']),
				'end_time'   => strtotime($post_data['end_time']),
				'status'     => $post_data['status'],
				'promotion_description' => $post_data['promotion_description']
			);
			
			$this->model_sale_promotion->editPromotion($this->request->get['promotion_id'], $params_data);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('sale/promotion', 'token=' . $this->session->data['token'], 'SSL'));
		}
    	$this->getForm_text();
    	$this->getForm_update();
  	}

  	private function getList() {
  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/promotion', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('sale/promotion/insert', 'token=' . $this->session->data['token'], 'SSL');
		
  		$url = '';
		if (isset($this->request->get['filter_promotion_id'])) {
			$promotion_id = $this->request->get['filter_promotion_id'];
			$url .= '&filter_promotion_id=' . $promotion_id;
		} else {
			$promotion_id = null;
		}
		if (isset($this->request->get['filter_name'])) {
			$name = $this->request->get['filter_name'];
			$url .= '&filter_name=' . urlencode(html_entity_decode($name, ENT_QUOTES, 'UTF-8'));
		} else {
			$name = null;
		}
		if (isset($this->request->get['filter_status'])) {
			$status = $this->request->get['filter_status'];
			$url .= '&filter_status=' . $status;
		} else {
			$status = null;
		}
		if (isset($this->request->get['filter_start_time'])) {
			$start_time = $this->request->get['filter_start_time'];
			$url .= '&filter_start_time=' . $start_time;
		} else {
			$start_time = null;
		}
		if (isset($this->request->get['filter_end_time'])) {
			$end_time = $this->request->get['filter_end_time'];
			$url .= '&filter_end_time=' . $end_time;
		} else {
			$end_time = null;
		}
		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'start_time';
		}
		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'DESC';
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}
		$data = array(
			'promotion_id' => $promotion_id,
			'name'         => $name,
			'status'       => $status,
			'start_time'   => $start_time == null ? null : strtotime($start_time),
			'end_time'     => $end_time == null ? null : strtotime($end_time),
			'sort'         => $sort,
			'order'        => $order,
			'start'        => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit'        => $this->config->get('config_admin_limit')
		);

  		if (isset($this->request->server['HTTPS']) && (($this->request->server['HTTPS'] == 'on') || ($this->request->server['HTTPS'] == '1'))) {
			$httpImg = HTTPS_IMAGE;
		} else {
			$httpImg = HTTP_IMAGE;
		}
		$total = $this->model_sale_promotion->getTotalPromotions($data);
		$results = $this->model_sale_promotion->getPromotions($data);
		$this->data['promotions'] = array();
    	foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('sale/promotion/update', 'token=' . $this->session->data['token'] . '&promotion_id=' . $result['promotion_id'], 'SSL')
			);
			$this->data['promotions'][] = array(
				'promotion_id' => $result['promotion_id'],
				'name'         => $result['name'],
				'display_name' => $result['display_name'],
				'image'        => $httpImg . $result['image'],
				'description'  => $result['description'],
				'start_time'   => $result['start_time'],
				'end_time'     => $result['end_time'],
				'status'       => $result['status'],
				'action'       => $action
			);
		}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_missing'] = $this->language->get('text_missing');

		$this->data['column_promotion_id'] = $this->language->get('column_promotion_id');
    	$this->data['column_name'] = $this->language->get('column_name');
    	$this->data['column_display_name'] = $this->language->get('column_display_name');
		$this->data['column_description'] = $this->language->get('column_description');
		$this->data['column_start_time'] = $this->language->get('column_start_time');
		$this->data['column_end_time'] = $this->language->get('column_end_time');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_image'] = $this->language->get('column_image');
		$this->data['column_action'] = $this->language->get('column_action');
		
		$this->data['text_status_all'] = $this->language->get('text_status_all');
		$this->data['text_status_yes'] = $this->language->get('text_status_yes');
		$this->data['text_status_no'] = $this->language->get('text_status_no');

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');

		$this->data['token'] = $this->session->data['token'];
		
		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}
		
  		if ($order == 'ASC') {
			$url_sort = $url . '&order=DESC';
		} else {
			$url_sort = $url . '&order=ASC';
		}
		$this->data['sort_status'] = $this->url->link('sale/promotion', 'token=' . $this->session->data['token'] . '&sort=status' . $url_sort, 'SSL');
		$this->data['sort_start_time'] = $this->url->link('sale/promotion', 'token=' . $this->session->data['token'] . '&sort=start_time' . $url_sort, 'SSL');
		$this->data['sort_end_time'] = $this->url->link('sale/promotion', 'token=' . $this->session->data['token'] . '&sort=end_time' . $url_sort, 'SSL');
		
  		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $sort;
		}
		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $order;
		}
		$pagination = new Pagination();
		$pagination->total = $total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('sale/promotion', 'token=' . $this->session->data['token'] . $url, 'SSL');

		$this->data['pagination'] = $pagination->render();

		$this->data['filter_promotion_id'] = $promotion_id;
		$this->data['filter_name'] = $name;
		$this->data['filter_status'] = $status == null ? -1 : $status;
		$this->data['filter_start_time'] = $start_time;
		$this->data['filter_end_time'] = $end_time;
		$this->data['sort'] = $sort;
		$this->data['order'] = $order;
		$this->template = 'sale/promotion_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render());
  	}

  	public function getForm_text() {
		$this->data['heading_title'] = $this->language->get('heading_title');
		 
		$this->data['text_no_results'] = $this->language->get('text_no_results');  
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_status_yes'] = $this->language->get('text_status_yes');
		$this->data['text_status_no'] = $this->language->get('text_status_no');
		
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_start_time'] = $this->language->get('entry_start_time');
		$this->data['entry_end_time'] = $this->language->get('entry_end_time');
		$this->data['entry_status'] = $this->language->get('entry_status');
		$this->data['entry_display_name'] = $this->language->get('entry_display_name');
		$this->data['entry_description'] = $this->language->get('entry_description');	
			
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		
		$this->data['token'] = $this->session->data['token'];
		
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		$this->data['breadcrumbs'] = array();
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
			'separator' => false
		);
		$this->data['breadcrumbs'][] = array(
			'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('sale/promotion', 'token=' . $this->session->data['token'], 'SSL'),				
			'separator' => ' :: '
		);
		
		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
  	}
	
	public function getForm_insert() {
		$this->data['action'] = $this->url->link('sale/promotion/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('sale/promotion', 'token=' . $this->session->data['token'], 'SSL');

		$this->document->addScript('view/javascript/jquery/ajaxupload.js');
		$this->load->model('tool/image');
		
      	$this->data['name'] = '';
      	$this->data['image'] = '';
      	$this->data['start_time'] = '';
      	$this->data['end_time'] = '';
      	$this->data['status'] = 0;
    	$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		
		$this->data['promotion_description'] = array();
		
		$this->template = 'sale/promotion_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render());
  	}
  	
	public function getForm_update() {
		$this->data['action'] = $this->url->link('sale/promotion/update', 'token=' . $this->session->data['token'] . "&promotion_id=" . $this->request->get['promotion_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('sale/promotion', 'token=' . $this->session->data['token'], 'SSL');

    	$promotion_info = $this->model_sale_promotion->getPromotion($this->request->get['promotion_id']);
    	$this->data['name'] = $promotion_info['name'];
      	$this->data['image'] = $promotion_info['image'];
      	$this->data['start_time'] = $promotion_info['start_time'];
      	$this->data['end_time'] = $promotion_info['end_time'];
      	$this->data['status'] = $promotion_info['status'];
      	
      	$this->load->model('tool/image');
      	if($promotion_info['image']){
    		$this->data['thumb'] = $this->model_tool_image->resize($promotion_info['image'], 100, 100);
      	}else{
    		$this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
      	}
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
		
		$this->data['promotion_description'] = $this->model_sale_promotion->getPromotionDescription($this->request->get['promotion_id']);
		
		$this->template = 'sale/promotion_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
		
		$this->response->setOutput($this->render());
  	}
}
?>